package com.eshore.norm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.eshore.norm.pojo.constant.Const;
import com.eshore.norm.pojo.dto.NormConfig;
import com.eshore.norm.pojo.dto.NormFlavor;
import com.eshore.norm.mapper.NormFlavorMapper;
import com.eshore.norm.pojo.dto.NormHost;
import com.eshore.norm.pojo.po.NormFlavorPO;
import com.eshore.norm.pojo.po.NormHypervisorHostPO;
import com.eshore.norm.pojo.vo.SyncCmdbHostVO;
import com.eshore.norm.service.INormFlavorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eshore.norm.util.DateUtils;
import com.github.xiaoymin.knife4j.core.conf.Consts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 规格列表 服务实现类
 * </p>
 *
 * @author chenjl
 * @since 2024-01-05
 */
@Service
public class NormFlavorServiceImpl extends ServiceImpl<NormFlavorMapper, NormFlavor> implements INormFlavorService {

    @Override
    public void dataImport(NormFlavorPO po) {
        Optional<NormFlavorPO> po_optional = Optional.ofNullable(po);
        po_optional.ifPresent(s -> {
            Optional<List<NormFlavorPO.FlavorsDTO>> dto_optional = Optional.ofNullable(s.getFlavors());
            dto_optional.ifPresent(item -> {
                List<String> newFlavorIds = item.stream().map(NormFlavorPO.FlavorsDTO::getId).collect(Collectors.toList());
                cleanOldData(Const.CONFIG_ID, newFlavorIds);
                //将po转化成实体对象入库
                List<NormFlavor> entityList = toNormFlavor(Const.CONFIG_ID, dto_optional);
                if (!CollectionUtils.isEmpty(entityList)) {
                    getBaseMapper().insertOrUpdateBatch(entityList);

                }
            });
        });
    }




    /**
     * 删除现已不存在的数据，保持同步一致
     *
     * @param configId
     * @param newFlavorIds 需要同步的索引Id集合
     * @author v-chenjl62
     * @date 2024/1/10
     */
    private void cleanOldData(Long configId, List<String> newFlavorIds) {
        List<String> oldFlavorIds = lambdaQuery().eq(NormFlavor::getConfigId, configId).list().stream().map(NormFlavor::getFlavorId).collect(Collectors.toList());
        //筛选出差异的imagesId即为多余的需要删除
        oldFlavorIds.removeAll(newFlavorIds);
        if (!CollectionUtils.isEmpty(oldFlavorIds)) {
            getBaseMapper().delete(new QueryWrapper<NormFlavor>().in("flavor_id", oldFlavorIds));
        }
    }

    private List<NormFlavor> toNormFlavor(Long configId, Optional<List<NormFlavorPO.FlavorsDTO>> list_optional) {
        return list_optional.get().stream().map(item -> {
            NormFlavor entity = new NormFlavor();
            BeanUtils.copyProperties( item,entity, "id","links","extraSpecs");
            entity.setFlavorId(item.getId());//flavorId 是唯一索引
            entity.setLinks(JSON.toJSONString(item.getLinks(), SerializerFeature.WriteMapNullValue));
            entity.setExtraSpecs(JSON.toJSONString(item.getExtraSpecs(), SerializerFeature.WriteMapNullValue));

            entity.setId(IdWorker.getId());
            entity.setConfigId(configId);
//            entity.setCreateDate(LocalDateTime.now());
//            entity.setUpdateDate(LocalDateTime.now());
            return entity;
        }).collect(Collectors.toList());
    }
}
